import React from 'react';
import { Router, Switch } from 'dva/router';
// 引入utils/SubRoutes.js中封装好的路由调用方法
import SubRoutes from './utils/SubRoutes'
// 声明私有路由变量
const isAuthority = true;
// 路由优化
const RouteConfig = [
  {  // 设置根路径
    path:'/',
    component:()=>import('./pages/IndexPage'),
    model:[],//引入./models/global.js中state状态信息,通过subRoutes.js传递给到Indexpage.js中使用
    routes:[//其它路由
      {
        path:"/home",
        component:()=>import('./pages/Home'),
        redirect:true, //重定向跳转
        model:[import('./models/home')],
        isAuthority
      },
      {
        path:"/menus",
        component:()=>import('./pages/Menus'),
        model:[],
        isAuthority
      },
      {
        path:"/about",
        component:()=>import('./pages/About'),
        model:[],
        isAuthority,
        routes:[//二级路由
          {
            path:'/about/history',
            model:[],
            component:()=>import('./pages/About/History.js')
          },
          {
            path:'/about/contact',
            model:[],
            component:()=>import('./pages/About/Contact.js'),
            // 三级路由
            routes:[
              {
                path: '/about/contact/phone',
                model: [],
                component: () => import('./pages/About/Phone')
              },
              {
                path: '/about/contact/address',
                model: [],
                component: () => import('./pages/About/Address')
              }
            ]
          },
          {
            path:'/about/orderingguide',
            model:[],
            component:()=>import('./pages/About/OrderingGuide.js')
          },
          {
            path:'/about/delivery',
            model:[],
            component:()=>import('./pages/About/Delivery.js')
          },
        ]
      },
      {
        path:"/admin",
        component:()=>import('./pages/Admin'),
        model:[],
        isAuthority
      },
      {
        path:"/login",
        component:()=>import('./pages/User/Login'),
        model:[],
      },
      {
        path:"/register",
        component:()=>import('./pages/User/Register'),
        model: [],
      },
    ]
  }
]

function RouterConfig({ history,app }) {
  // console.log(app)
  return (
    <Router history={history}>
      <Switch>
       { 
         RouteConfig.map((route,i) =>(/* 调用在utils中封装的组件,把 RouteConfig中定义的所有路由传入 ,进行渲染*/
          <SubRoutes key={i} {...route} app={app} />
        ))
      }
      </Switch>
    </Router>
  );
}

export default RouterConfig;
